package com.sxsihe.heartmate.util;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.Toast;
import com.sxsihe.heartmate.GetBackPwdActivity;
import com.sxsihe.heartmate.MainActivity;
import com.sxsihe.heartmate.UserLoginActivity;
import com.sxsihe.heartmate.UserRegisterActivity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by mgxc2 on 15/11/30.
 */
public class HttpPostUtil extends AsyncTask<String, Void, String> {

    Context context;
    String method;
    public HttpPostUtil(Context context,String method) {
        this.context=context;
        this.method = method;
    }


    public  String post(String url,ArrayList<NameValuePair> params) {
        String strResult = "";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        PersistentCookieStore cookieStore = new PersistentCookieStore(context);
        httpclient.setCookieStore(cookieStore);
        HttpPost httpRequest =new HttpPost(url);
        try{
            httpRequest.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
            //发出HTTP request
            httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            //取得HTTP response
            HttpResponse httpResponse= httpclient.execute(httpRequest);
            //若状态码为200 ok
            if(httpResponse.getStatusLine().getStatusCode()==200){
                //取出回应字串
                strResult= EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
                List<Cookie> cookies = httpclient.getCookieStore().getCookies();
                PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
                for (Cookie cookie:cookies){
                    myCookieStore.addCookie(cookie);
                }
            }else{
                strResult = "Error Response"+httpResponse.getStatusLine().toString();
            }
        }catch(ClientProtocolException e){
           e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strResult;
    }

    @Override
    protected String doInBackground(String... args) {
        String url = args[0];
        ArrayList<NameValuePair> params = new ArrayList<>();
        if("login".equals(method)){
            params.add(new BasicNameValuePair("loginname",args[1]));
            params.add(new BasicNameValuePair("password", args[2]));
        }else if("updateUserInfo".equals(method)){
            params.add(new BasicNameValuePair("id",args[1]));
            params.add(new BasicNameValuePair("name",args[2]));
            params.add(new BasicNameValuePair("password", args[3]));
            params.add(new BasicNameValuePair("birthday", args[4]));
            params.add(new BasicNameValuePair("gender", args[5]));
            params.add(new BasicNameValuePair("linkman", args[6]));
            params.add(new BasicNameValuePair("linkmanname", args[7]));
        }else if("register".equals(method)){
            params.add(new BasicNameValuePair("phone",args[1]));
            params.add(new BasicNameValuePair("password",args[2]));
        }else if("getUserInfo".equals(method)){

        }else if("getBackPassword".equals(method)){
            params.add(new BasicNameValuePair("phone",args[1]));
            params.add(new BasicNameValuePair("password", args[2]));
        }
        return post(url, params);
    }



    protected void onPostExecute(String result) {
        if("login".equals(method)){
            ((UserLoginActivity)context).loginResult(result);
        }else if("updateUserInfo".equals(method)){
            ((MainActivity)context).updateUserInfoResult(result);
        }else if("register".equals(method)){
            ((UserRegisterActivity)context).registerResult(result);
        }else if("getUserInfo".equals(method)){
            ((MainActivity)context).getUserInfo(result);
        }else if("getBackPassword".equals(method)){
            ((GetBackPwdActivity)context).getPasswordResult(result);
        }
    }
}
